package com.hjq.fcloud.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.blankj.utilcode.util.ClickUtils
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.hjq.fcloud.R
import com.hjq.fcloud.camear.BitmapSaver
import com.hjq.fcloud.event.EventBusUtils
import com.hjq.fcloud.utils.CommonUploadUtils
import com.hjq.fcloud.widget.HandwritingBoardView
import kotlinx.android.synthetic.main.activity_start_sign.clearTv
import kotlinx.android.synthetic.main.activity_start_sign.finishTv
import kotlinx.android.synthetic.main.activity_start_sign.signPanelView

/**
 * 签名
 */
class SignActivity : BaseDetailActivity() {

    companion object {
        //人脸认证token
        val INTENT_OUT_FACE_AUTH_TOKEN = "auth_token"

        fun launcher(context: Context, bundle: Bundle?) {
            var intent = Intent(context, SignActivity::class.java)
            if (bundle != null) {
                intent.putExtras(bundle)
            }
            context.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_start_sign
    }

    override fun initView() {
        ClickUtils.applySingleDebouncing(clearTv,{
            signPanelView.redo()
        })

        ClickUtils.applySingleDebouncing(finishTv,{
            if (signPanelView.bitmap == null) {
                showToast("签名数据不能为空")
                return@applySingleDebouncing
            }

            setResult(signPanelView)
        })
    }

    override fun initData() {

    }

    private fun setResult(signPanelView: HandwritingBoardView) {
        showDialog()
        var fileName = System.currentTimeMillis().toString().plus(".png")
        var filePath = BitmapSaver.saveToInternalStorage(this,signPanelView!!.bitmap!!, fileName)
        if(filePath!= null){
            CommonUploadUtils.uploadFile(this, filePath, { success, errorMsg, items ->
                hideDialog()
                if (success) {
                    var url = items!!.get(0).url
                    setResult(url)
                } else {
                    showToast(errorMsg)
                }
            })
        }
    }

    /**
     * 提交
     */
    private fun setResult(signature_pic: String) {
        var retMap = HashMap<String,String?>()
        retMap.put(INTENT_OUT_FACE_AUTH_TOKEN,getString(INTENT_OUT_FACE_AUTH_TOKEN))
        retMap.put("signature_pic",signature_pic)

        EventBusUtils.sendEventBefromFaceAndSignFinish(retMap)
        finish()
    }


}